package com.cby.operation.common.ui

import android.app.DialogFragment
import android.app.FragmentManager
import android.os.Bundle
import android.view.*
import com.cby.operation.common.ext.getLayoutId
import com.cby.operation.common.ext.getStyleId

/**
 * Description:HykbBaseDialogFragment
 *
 * @author 陈宝阳
 * @create 2020/8/6 10:11
 */
abstract class BaseDialogFragment : DialogFragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        UIStackManager.instance!!.addFragment(this.tag, this)
        setStyle(STYLE_NO_TITLE, getStyleId("CommonDlgTheme"))
        parseArgs()
    }

    override fun onStart() {
        super.onStart()
        if (dialog != null) {
            dialog!!.window
                ?.setLayout(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            dialog!!.window!!.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
            dialog!!.window!!.setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                        or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
            )
            setSystemUi()
            dialog!!.setCanceledOnTouchOutside(false)
            dialog!!.setCancelable(false)
            dialog!!.setOnKeyListener { dialog, keyCode, event ->
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    //拦截返回键事件
                    isInterceptBackEvent()
                } else false
            }
        }
    }

    protected open fun isInterceptBackEvent() = true

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(activity!!.getLayoutId(layoutName()), container)
        initView(view)
        setContent()
        initListener()
        return view
    }

    protected abstract fun parseArgs()
    protected abstract fun layoutName(): String
    protected abstract fun initView(view: View?)
    protected abstract fun setContent()
    protected abstract fun initListener()
    protected open fun setSystemUi() {
        var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or  //布局位于状态栏下方
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or  //全屏
                View.SYSTEM_UI_FLAG_FULLSCREEN or  //隐藏导航栏
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        uiOptions = uiOptions or 0x00001000
        dialog!!.window!!.decorView.systemUiVisibility = uiOptions
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            val c = Class.forName("android.app.DialogFragment")
            val con = c.getConstructor()
            val obj = con.newInstance()
            val dismissed = c.getDeclaredField("mDismissed")
            dismissed.isAccessible = true
            dismissed[obj] = false
            val shownByMe = c.getDeclaredField("mShownByMe")
            shownByMe.isAccessible = true
            shownByMe[obj] = false
        } catch (e: Exception) {
            e.printStackTrace()
        }
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }

    override fun dismiss() {
        dismissAllowingStateLoss()
    }

    override fun onDestroy() {
        UIStackManager.instance!!.removeFragment(this.tag)
        super.onDestroy()
    }
}